package com.jy.rhin.facade.cda;

import com.jy.framework.base.utils.Serializer;
import com.jy.rhin.parse.CDAParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author WeiQ
 * @date 2023/3/29 9:57
 */
@Slf4j
@RequestMapping("/test")
@Api(tags = "测试cda解析功能", hidden = true)
@RestController
public class TestParseController {

    @ApiOperation("解析")
    @GetMapping("/parse")
    public String valid(String name) {
        String doc = getDoc(name);
        log.info("解析文档:{}", doc);
        CDAParser cdaParser = new CDAParser(doc);
        Object o = cdaParser.parse();
        return Serializer.serialize(o);
    }

    String getDoc(String name) {
        try {
            return IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("cda/example/" + name));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
